package com.atguigu.utils;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;

import java.util.List;


public class VodTest {
    public static void main(String[] args) {
        getVideoURL();
        getVideoAuth();
        uploadVideo("LTAI4FvvVEWiTJ3GNJJqJnK7","9st82dv7EvFk9mTjYO1XXbM632fRbG","testVideo","D://test/1.mp4");

    }
    public static DefaultAcsClient initVodClient(String accessKeyId,String accessKeySecrecy){
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecrecy);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;

    }
    public static void getVideoURL(){
        DefaultAcsClient client = VodTest.initVodClient("LTAI4FvvVEWiTJ3GNJJqJnK7","9st82dv7EvFk9mTjYO1XXbM632fRbG");

        // 创建API请求并设置参数
        GetPlayInfoRequest request = new GetPlayInfoRequest();

        request.setVideoId("g7fs7afs7f89s79");


        try {
            GetPlayInfoResponse response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            for(GetPlayInfoResponse.PlayInfo playInfo:playInfoList){
                System.out.println(playInfo.getPlayURL());
            }

        } catch (ServerException e) {
            throw new RuntimeException(e);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }
    public static void getVideoAuth(){
        DefaultAcsClient client = VodTest.initVodClient("LTAI4FvvVEWiTJ3GNJJqJnK7","9st82dv7EvFk9mTjYO1XXbM632fRbG");

        // 创建API请求并设置参数
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

        request.setVideoId("g7fs7afs7f89s79");


        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            System.out.println(playAuth);

        } catch (ServerException e) {
            throw new RuntimeException(e);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }
    public static String uploadVideo(String accessKeyId,String accessKeySecret,String videoName,String filePath){


        // 创建API请求并设置参数
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId,accessKeySecret,videoName,filePath);

        request.setPartSize(2*1024*1024L);
        request.setTaskNum(1);
        UploadVideoImpl uploader = new UploadVideoImpl();



        UploadVideoResponse response = uploader.uploadVideo(request);
        return response.getVideoId();


    }


}
